package edu.co.gap.view;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import edu.co.gap.view.webservice.webs;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity implements OnClickListener {

	// constants
	public static final String NAME_CAPTURE = "captureInfo";
	public static final String FORMAT = "jpg";
	public static final String IMAGE_NAME = NAME_CAPTURE+"."+FORMAT;
	// atributes from view
	private TextView txtExplination;
	private Button btnIdentify, btnExit;

	// atributes
	private static int TAKE_PICTURE = 1;
	private Uri outputFileUri;
	private OutputStream filoutputStream;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnIdentify = (Button) findViewById(R.id.idBtnIdentify);
		btnIdentify.setOnClickListener(this);
		btnIdentify = (Button) findViewById(R.id.idBtnExit);
		btnIdentify.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		saveFullImage();
	}

	private void saveFullImage() {

		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(Environment.getExternalStorageDirectory(),
				NAME_CAPTURE.concat(".").concat(FORMAT));

		try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		outputFileUri = Uri.fromFile(file);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		startActivityForResult(intent, TAKE_PICTURE);
	}
	
	@Override
	protected void onActivityResult(int requestCode,
	int resultCode, Intent data) {
		if (requestCode == TAKE_PICTURE) {
		Uri imageUri = null;
		// Check if the result includes a thumbnail Bitmap
		if (data != null) {
		if (data.hasExtra("data")) {
		Bitmap thumbnail = data.getParcelableExtra("data");
		// TODO Do something with the thumbnail
		}
		}
		else {
	//		filoutputStream=new OutputStream();
		// TODO Do something with the full image stored
		// in outputFileUri
		}
		}
		String str=new String("Identificando......");
		  Toast.makeText(getBaseContext(), str, 
	       		Toast.LENGTH_LONG).show();
		  
		  callCameraControl();
	}
	
	private void callCameraControl() {
		Intent myIntent = new Intent(this,webs.class);
//	Intent myIntent = new Intent(this, CameraView.class);
	startActivity(myIntent);
	}
	
}